<template>
  <div class="add">
    <el-dialog title="新增员工" :visible="showDailog">
      <el-form
        ref="myform"
        :rules="rules"
        :model="formData"
        label-width="100px"
      >
        <el-form-item label="姓名" prop="username">
          <el-input v-model="formData.username" placeholder="1-30个字符" />
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="formData.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="formData.timeOfEntry"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select v-model="formData.formOfEmployment" placeholder="请选择">
            <el-option
              v-for="item in hireType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="formData.workNumber" placeholder="1-20个字符" />
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-input
            v-model="formData.departmentName"
            placeholder="请选择"
            readonly
            @focus="openTree"
          />
          <el-tree
            v-if="treeshow"
            :props="defaultProps"
            class="mytree"
            default-expand-all
            :data="treedata"
            @node-click="nodeclick"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import employees from '@/utils//employees'
// import { getDepartments } from '../../'
import { getDepartments } from '../../../api/departments'
import { department } from '../../../utils/index'
import { addEmployee } from '../../../api/employees'
export default {
  //   props: ['showDailog'],
  props: {
    showDailog: {
      type: Boolean
    }
  },
  data() {
    return {
      // 聘用形式的数组
      hireType: employees.hireType,
      // 控制树形结构的显示与隐藏
      treeshow: false,
      // 树形结构的数据源
      treedata: [
        // {
        //   label: '北京',
        //   children: [
        //     { label: '朝阳', children: [{ label: '朝阳群众' }] },
        //     { label: '顺义', children: [{ label: '顺义群众' }] }
        //   ]
        // },
        // {
        //   label: '深圳'
        // }
      ],
      defaultProps: {
        label: 'name'
      },
      // 表单元素的属性
      formData: {
        username: '', // string	非必须		姓名
        mobile: '', // string	非必须		手机号
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', // string	非必须		工号
        departmentName: '', // string	非必须		组织名称
        timeOfEntry: '' // string	非必须		入职时间
      },
      // 表单元素的验证规则
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    cancel() {
      this.$refs.myform.resetFields()
      this.formData = {
        username: '', // string	非必须		姓名
        mobile: '', // string	非必须		手机号
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', // string	非必须		工号
        departmentName: '', // string	非必须		组织名称
        timeOfEntry: '' // string	非必须		入职时间
      }
      this.$emit('update:showDailog', false)
    },
    async getDepartments() {
      const res = await getDepartments()
      // console.log(res)
      this.treedata = department(res.depts, '')
    },
    submit() {
      this.$refs.myform.validate(async res => {
        // console.log(res)
        if (res) {
          await addEmployee(this.formData)
          this.$emit('update:showDailog', false)
          this.$emit('addEmployee')
          this.formData = {
            username: '', // string	非必须		姓名
            mobile: '', // string	非必须		手机号
            formOfEmployment: '', //	number	非必须		聘用形式
            workNumber: '', // string	非必须		工号
            departmentName: '', // string	非必须		组织名称
            timeOfEntry: '' // string	非必须		入职时间
          }
        }
      })
    },
    // 点击树状态结构中的节点时会触发
    nodeclick(e) {
      // e.label：被点击元素文本内容
      // 赋值给上面的输入框
      this.formData.departmentName = e.name
      // 关闭树状结构
      this.treeshow = false
    },
    openTree() {
      // 显示树形结构
      this.treeshow = true
    }
  }
}
</script>

<style lang="scss">
.add {
  .mytree {
    position: absolute;
    width: 100%;
    border: 1px solid #ccc;
    border-radius: 6px;
  }
}
</style>
